package com.ktgj.flight.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.flight.domain.vo.AddFlightChannelRuleVo;
import com.ktgj.flight.domain.vo.FlightChannelRuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.flight.domain.FlightChannelRule;
import com.ktgj.flight.service.IFlightChannelRuleService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 出票渠道规定主Controller
 * 
 * @author ruoyi
 * @date 2021-12-23
 */
@RestController
@RequestMapping("/rule")
public class FlightChannelRuleController extends BaseController
{
    @Autowired
    private IFlightChannelRuleService flightChannelRuleService;

    /**
     * 查询出票渠道规定主列表
     */
    @PreAuthorize(hasPermi = "flight:rule:list")
    @GetMapping("/list")
    public TableDataInfo list(FlightChannelRule flightChannelRule)
    {
        startPage();
        List<FlightChannelRule> list = flightChannelRuleService.selectFlightChannelRuleList(flightChannelRule);
        return getDataTable(list);
    }

    /**
     * 导出出票渠道规定主列表
     */
    @PreAuthorize(hasPermi = "flight:rule:export")
    @Log(title = "出票渠道规定主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FlightChannelRule flightChannelRule) throws IOException
    {
        List<FlightChannelRule> list = flightChannelRuleService.selectFlightChannelRuleList(flightChannelRule);
        ExcelUtil<FlightChannelRule> util = new ExcelUtil<FlightChannelRule>(FlightChannelRule.class);
        util.exportExcel(response, list, "出票渠道规定主数据");
    }

    /**
     * 获取出票渠道规定主详细信息
     */
//    @PreAuthorize(hasPermi = "flight:rule:query")
    @PostMapping("/getInfo")
    public AjaxResult getInfo(@RequestBody Map<String, Long> map)
    {
        Long ruleId = map.get("ruleId");
        return AjaxResult.success(flightChannelRuleService.selectFlightChannelRuleById(ruleId));
    }

    /**
     * 新增出票渠道规定主
     */
    @PreAuthorize(hasPermi = "flight:rule:add")
    @Log(title = "出票渠道规定主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FlightChannelRule flightChannelRule)
    {
        return toAjax(flightChannelRuleService.insertFlightChannelRule(flightChannelRule));
    }

    /**
     * 修改出票渠道规定主
     */
    @PreAuthorize(hasPermi = "flight:rule:edit")
    @Log(title = "出票渠道规定主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FlightChannelRule flightChannelRule)
    {
        return toAjax(flightChannelRuleService.updateFlightChannelRule(flightChannelRule));
    }

    /**
     * 删除出票渠道规定主
     */
    @PreAuthorize(hasPermi = "flight:rule:remove")
    @Log(title = "出票渠道规定主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ruleIds}")
    public AjaxResult remove(@PathVariable Long[] ruleIds)
    {
        return toAjax(flightChannelRuleService.deleteFlightChannelRuleByIds(ruleIds));
    }

    /**
     * 删除出票渠道规定主
     */
    @PostMapping("/delRemove")
    public AjaxResult delRemove(@RequestBody Map<String, Object> map) {
        Long ruleId = Long.valueOf(map.get("ruleId").toString());
        int i = flightChannelRuleService.deleteFlightChannelRuleById(ruleId);
        if (i > 0){
            return AjaxResult.success("删除成功");
        }
        else {
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 出票规定列表
     * */
    @PostMapping("/selectFlightChannelRuleBy")
    public TableDataInfo selectFlightChannelRuleBy(@RequestBody FlightChannelRuleVo flightChannelRuleVo) {
        startPage();
        List<FlightChannelRule> flightChannelRules = flightChannelRuleService.selectFlightChannelRuleBy(flightChannelRuleVo);
        return getDataTable(flightChannelRules);
    }

    /**
     * 新建出票规定
     * */
    @PostMapping("/addFlightChannelRule")
    public AjaxResult addFlightChannelRule(HttpServletRequest request, @RequestBody AddFlightChannelRuleVo addFlightChannelRuleVo){
        int i;
        try {
            i = flightChannelRuleService.addFlightChannelRule(request, addFlightChannelRuleVo);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (i > 0){
            return AjaxResult.success("添加成功");
        }
        else {
            return AjaxResult.error("添加失败");
        }
    }
}
